//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartScaleBreaksView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartScaleBreaksView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartScaleBreaksView

IMPLEMENT_DYNCREATE(CChartScaleBreaksView, CBCGPChartExampleView)

CChartScaleBreaksView::CChartScaleBreaksView()
	: CBCGPChartExampleView(CChartScaleBreaksView::IDD)
{
	//{{AFX_DATA_INIT(CChartScaleBreaksView)
	m_nBreakStyle = 4;
	m_nFirstBreakOffsetPercent = 0;
	m_nGap = 0;
	m_bIsAutoMode = TRUE;
	m_nValueThresholdPercent = 1;
	m_strGapVal = _T("");
	m_strFirstBreakVal = _T("");
	//}}AFX_DATA_INIT
}

CChartScaleBreaksView::~CChartScaleBreaksView()
{
}

void CChartScaleBreaksView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartScaleBreaksView)
	DDX_Control(pDX, IDC_VALUE_THRESHOLD_PERCENT, m_wndValueThresholdPercent);
	DDX_Control(pDX, IDC_LABEL1, m_wndLabel1);
	DDX_Control(pDX, IDC_FIRST_BREAK_OFFSET_PERCENT, m_wndFirstBreakOffsetPercent);
	DDX_Control(pDX, IDC_GAP, m_wndGap);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_BREAK_STYLE, m_nBreakStyle);
	DDX_Slider(pDX, IDC_FIRST_BREAK_OFFSET_PERCENT, m_nFirstBreakOffsetPercent);
	DDX_Slider(pDX, IDC_GAP, m_nGap);
	DDX_Check(pDX, IDC_SCALEBREAK_AUTO_MODE, m_bIsAutoMode);
	DDX_CBIndex(pDX, IDC_VALUE_THRESHOLD_PERCENT, m_nValueThresholdPercent);
	DDX_Text(pDX, IDC_GAP_VALUE, m_strGapVal);
	DDX_Text(pDX, IDC_FIRST_BREAK_VALUE, m_strFirstBreakVal);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartScaleBreaksView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartScaleBreaksView)
	ON_CBN_SELENDOK(IDC_BREAK_STYLE, OnUpdateChart)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_SCALEBREAK_AUTO_MODE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_VALUE_THRESHOLD_PERCENT, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartScaleBreaksView diagnostics

#ifdef _DEBUG
void CChartScaleBreaksView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartScaleBreaksView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartScaleBreaksView message handlers

void CChartScaleBreaksView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_wndGap.SetRange(4, 20);
	m_nGap = 7;

	m_wndFirstBreakOffsetPercent.SetRange(1, 80);
	m_nFirstBreakOffsetPercent = 70;

	UpdateData(FALSE);

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartArea);
	pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);
	pChart->SetThemeOpacity(70);

	pChart->AddChartData(0.0012);
	pChart->AddChartData(0.0015);
	pChart->AddChartData(0.0018);
	pChart->AddChartData(0.0016);

	pChart->AddChartData(110);
	pChart->AddChartData(125);
	pChart->AddChartData(100);
	pChart->AddChartData(120);

	pChart->AddChartData(10000);
	pChart->AddChartData(12000);
	pChart->AddChartData(11000);
	pChart->AddChartData(11500);

	pChart->AddChartData(130);
	pChart->AddChartData(115);
	pChart->AddChartData(75);

	pChart->AddChartData(0.0013);
	pChart->AddChartData(0.0007);

	pChart->GetSeries(0)->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);
	pChart->ShowDataMarkers(TRUE, 4);

	OnUpdateChart();
}

void CChartScaleBreaksView::OnUpdateChart()
{
	UpdateData();

	m_wndLabel1.EnableWindow(m_bIsAutoMode);
	m_wndValueThresholdPercent.EnableWindow(m_bIsAutoMode);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartAxis* pAxisY = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);

	CBCGPChartAxisScaleBreak::AxisScaleBreakStyle breakStyle = (CBCGPChartAxisScaleBreak::AxisScaleBreakStyle) m_nBreakStyle;

	CBCGPChartScaleBreakOptions sbOptions;

	switch (m_nValueThresholdPercent)
	{
	case 0:
		sbOptions.m_dblValueThresholdPercent = 1;
		break;

	case 1:
		sbOptions.m_dblValueThresholdPercent = 50;
		break;

	case 2:
		sbOptions.m_dblValueThresholdPercent = 90;
		break;
	}

	sbOptions.m_bAutomatic = m_bIsAutoMode;
	sbOptions.m_nGap = m_nGap;
	sbOptions.m_dblFirstBreakOffsetPercent = m_nFirstBreakOffsetPercent;
	sbOptions.m_nMaxCount = 10;
	sbOptions.m_style = breakStyle;

	if (m_bIsAutoMode)
	{
		pAxisY->EnableScaleBreaks(TRUE, &sbOptions);
	}
	else
	{
		pAxisY->RemoveAllBreaks();
		pAxisY->EnableScaleBreaks(TRUE, &sbOptions, FALSE);

		CBCGPChartAxisScaleBreak sb;
		sb.m_nGap = m_nGap;
		sb.m_dblOffsetPercent = m_nFirstBreakOffsetPercent;
		sb.m_scaleBreakStyle = breakStyle;

		sb.m_dblStart = 0.0020;
		sb.m_dblEnd = 60;

		pAxisY->AddScaleBreak(sb, FALSE);

		sb.m_dblStart = 140;
		sb.m_dblEnd = 8000;

		pAxisY->AddScaleBreak(sb, TRUE);

		pChart->SetDirty(TRUE, TRUE);
	}

	m_strGapVal.Format(_T("%d pixels"), m_nGap);
	m_strFirstBreakVal.Format(_T("%d %%"), m_nFirstBreakOffsetPercent);

	UpdateData(FALSE);
}

void CChartScaleBreaksView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (pScrollBar != NULL)
	{
		OnUpdateChart();
		return;
	}

	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);
}
